package apollo.generator.modules.controller.generate;

import apollo.generator.common.annotation.SysLog;
import apollo.generator.common.base.BaseController;
import apollo.generator.common.base.R;
import apollo.generator.common.base.RR;
import apollo.generator.modules.entity.TableEntity;
import apollo.generator.modules.service.GenerateService;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <Description>
 * 代码生成处理器
 * @author Jun Lee
 * @version 1.0
 * @createDate 2020/07/02 15:30
 * @see apollo.generator.modules.controller.generate
 */
@RestController
@RequestMapping("generator")
public class GenerateController extends BaseController {

    @Autowired
    private GenerateService generateService;

    /**
     * 查询数据库列表
     * @param params
     * @return
     * @throws Exception
     */
    @RequestMapping("selectDataBasePage")
    @RequiresPermissions("generator:selectDataBasePage")
    public R selectDataBasePage(@RequestParam Map<String, Object> params) throws Exception {
        return RR.success(generateService.selectDataBasePage(params));
    }

    /**
     * 查询数据库表
     * @param params
     * @return
     * @throws Exception
     */
    @SysLog("查询数据库表")
    @RequestMapping("selectTablePage")
    @RequiresPermissions("generator:selectTablePage")
    public R selectTablePage(@RequestParam Map<String, Object> params) throws Exception {
        return RR.success(generateService.selectTablePage(params));
    }


    /**
     * 整库代码生成
     * @param params
     * @return
     * @throws Exception
     */
    @SysLog("后端整库代码生成")
    @RequestMapping("allGenerateCode")
    @RequiresPermissions("generator:allGenerateCode")
    public void allGenerateCode(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        //查询库的所有表信息
        ArrayList<TableEntity> tableEntities = generateService.selectTable(params);
        String str = tableEntities.stream().map(TableEntity::getTableName).collect(Collectors.joining(","));
        params.put("tables",str);
        byte[] bytes = generateService.generateCode(params);
        download2Zip(response,bytes);
    }


    /**
     * 代码生成
     * @param params
     * @return
     * @throws Exception
     */
    @SysLog("后端代码生成")
    @RequestMapping("generateCode")
    @RequiresPermissions("generator:generateCode")
    public void generateCode(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        byte[] bytes = generateService.generateCode(params);
        download2Zip(response,bytes);
    }

    /**
     * zpi下载
     * @param response
     * @param bytes
     * @throws Exception
     */
    private void download2Zip(HttpServletResponse response,byte[] bytes) throws Exception {
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename="+System.currentTimeMillis()+".zip");
        response.addHeader("Content-Length", "" + bytes.length);
        response.setContentType("application/octet-stream; charset=UTF-8");
        IOUtils.write(bytes, response.getOutputStream());
    }
}
